var isAdd = false;
var dataCodeGrid, dataCodeStore, dataCodeDialog;
$(function () {

    initGrid();
    initDialog();
});
function initGrid() {

    var Grid = BUI.Grid,
        Store = BUI.Data.Store,
        Format = Grid.Format,
        columns = [
            {title: '字典编码', width: 100, dataIndex: 'oriCodeId'},
            {title: '字典名称', width: 100, dataIndex: 'oriCodeName'},
            {title: '字典值', width: 100, dataIndex: 'oriCodeValue'}
        ];

    dataCodeStore = new Store({
        url: ctx + "/dataCode/admin/selectPageInfo",
        autoLoad: true,
        root: 'list',               //存放数据的字段名(rows)
        totalProperty: 'total',
        pageSize: 10	// 配置分页数目
    });
    dataCodeGrid = new Grid.Grid({
        render: '#dataCodeGrid',
        columns: columns,
        loadMask: true,
        forceFit: true,
        store: dataCodeStore,
        bbar: {
            // pagingBar:表明包含分页栏
            pagingBar: true
        },
        plugins: [Grid.Plugins.CheckSelection, Grid.Plugins.AutoFit], //勾选插件、自适应宽度插件
        tbar: {
            items: [{
                btnCls: 'button button-primary button-small',
                text: '修改',
                handler: editdataCode
            }]
        }
    });
    dataCodeGrid.render();
    //创建表单，表单中的日历，不需要单独初始化

    BUI.use(['bui/grid', 'bui/data', 'bui/form'], function (Grid, Data, Form) {
        var form = new Form.HForm({
            srcNode: '#dataCodeSearchForm'
        });
        form.on('beforesubmit', function (ev) {
            //序列化成对象
            var obj = form.serializeToObject();
            obj.start = 0; //返回第一页
            dataCodeStore.load(obj);
            return false;
        });
        form.render();
    })

    dataCodeStore.load();
}
function initDialog() {

    dataCodeEditForm = Bui.formRender("dataCodeEditForm");
    BUI.use('bui/overlay', function (Overlay) {
        dataCodeDialog = new Overlay.Dialog({
            id: 'dataCodeEditFormDialog',
            title: '修改收益信息',
            width: 500,
            height: 350,
            //配置DOM容器的编号
            contentId: 'dataCodeEditFormDialogContent',
            buttons: [
                {
                    text: '保存',
                    elCls: 'button button-primary',
                    handler: function () {
                        if (!dataCodeEditForm.isValid()) {
                            return;
                        }
                        var formVal = dataCodeEditForm.serializeToObject();
                        if(isAdd){
                            formVal.is=null;
                            formAction(formVal, ctx + "/dataCode/admin/insertDataCode");
                        }else{
                            formAction(formVal, ctx + "/dataCode/admin/updateDataCode");
                        }


                    }
                }, {
                    text: '关闭',
                    elCls: 'button',
                    handler: function () {
                        this.close();
                    }
                }
            ]
        });
    });
}
function editdataCode() {
    var selections = dataCodeGrid.getSelection();
    isAdd = false
    if (selections.length == 0) {
        BUI.Message.Alert('请选择一条记录！', function () {
        }, 'info');
        return;
    }
    if (selections.length > 1) {
        BUI.Message.Alert('只能选择一条记录！', function () {
        }, 'info');
        return;
    }

    document.getElementById("dataCodeEditForm").reset();
    dataCodeEditForm.setRecord(selections[0]);

    dataCodeDialog.set("title", "修改字典信息");
    dataCodeDialog.show();


}

function formAction(formVal, actionUrl) {
    CommonClass.AjaxAction(actionUrl, formVal, function (data) {
        if (data.success) {
            Message.show(data.msg, "success", 2000);
            dataCodeStore.load()
            dataCodeDialog.close();
        } else {
            Message.show(data.msg, "error", 2000);
        }
    });
}